package com.talkingdata.hadoop.guard.controller;

import com.talkingdata.hadoop.guard.bean.*;
import com.talkingdata.hadoop.guard.mapper.ClusterAliasMapper;
import com.talkingdata.hadoop.guard.mapper.DfsClusterMapper;
import com.talkingdata.hadoop.guard.mapper.NamenodeMapper;
import com.talkingdata.hadoop.guard.util.LoadConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.ArrayList;
import java.util.HashMap;

@Controller

public class IndexController {

    @Autowired
    private LoadConfig loadConfig;
    @Autowired
    private DfsClusterMapper dfsClusterMapper;
    @Autowired
    private NamenodeMapper namenodeMapper ;

    @Autowired
    private ClusterAliasMapper cam ;

    @GetMapping({"/","/index","/index.html"})
    public String index(Model model) {
        ArrayList <IndexPage>  indexPageList = new ArrayList<>();
        ArrayList<XmlConfig> configure = loadConfig.readxml();
        for (XmlConfig xc : configure) {
            DfsClusterState dfsClusterState = null;
            NameNodeState nameNodeState = null;
            ClusterAlias clusterAlias = null;
            try {
                dfsClusterState = dfsClusterMapper.selectDfsClusterStateByServiceId(xc.getServiceId());
                nameNodeState = namenodeMapper.selectActiveNameNodeStatebyServiceId(xc.getServiceId());
                clusterAlias = cam.selectClusterAliasByClusterhdfsId(xc.getServiceId());
                System.out.println(clusterAlias.toString());
            } catch (Exception e) {
                System.out.println(xc.getServiceId() + " dfsClusterState or nameNodeState conn db failed ");
                e.printStackTrace();
            }
            IndexPage ip= new IndexPage(dfsClusterState,nameNodeState,clusterAlias);
            indexPageList.add(ip);
        }
        model.addAttribute("indexPageList",indexPageList);
        return "index";
    }
}
